package com.roads.site.service.impl;

import com.roads.common.config.MasterConfig;
import com.roads.common.utils.DateUtils;
import com.roads.common.utils.file.FileUtils;
import com.roads.site.domain.SiteLabourStaffContract;
import com.roads.site.domain.SiteLabourStaffContractAnnex;
import com.roads.site.mapper.SiteLabourStaffContractAnnexMapper;
import com.roads.site.mapper.SiteLabourStaffContractMapper;
import com.roads.site.service.ISiteLabourStaffContractAnnexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 劳务工-合同附件Service业务层处理
 * 
 * @author nhp
 * @date 2023-03-24
 */
@Service
public class SiteLabourStaffContractAnnexServiceImpl implements ISiteLabourStaffContractAnnexService {

    @Autowired
    private SiteLabourStaffContractAnnexMapper mapper;

    @Autowired
    private SiteLabourStaffContractMapper contractMapper;

    /**
     * 根据主键ID查询详情
     * 
     * @param id 劳务工-合同附件主键
     * @return 劳务工-合同附件
     */
    @Override
    public SiteLabourStaffContractAnnex getModelById(Integer id) {
        return mapper.getModelById(id);
    }

    /**
     * 查询列表
     * 
     * @param model 劳务工-合同附件
     * @return 劳务工-合同附件
     */
    @Override
    public List<SiteLabourStaffContractAnnex> selectModelList(SiteLabourStaffContractAnnex model) {
        return mapper.selectModelList(model);
    }

    /**
     * 新增
     * 
     * @param model 劳务工-合同附件
     * @return 结果
     */
    @Override
    public int insert(SiteLabourStaffContractAnnex model) {
        model.setCreateTime(DateUtils.getNowDate());
        return mapper.insert(model);
    }

    /**
     * 修改
     * 
     * @param model 劳务工-合同附件
     * @return 结果
     */
    @Override
    public int update(SiteLabourStaffContractAnnex model) {
        return mapper.update(model);
    }

    /**
     * 批量删除
     * 
     * @param ids 主键集合
     * @return 结果
     */
    @Override
    public int deleteByIds(Integer[] ids) {
        return mapper.deleteByIds(ids);
    }

    /**
     * 删除劳务工-合同附件
     * 1.删除文件
     * 2.删除文件记录
     * 3.合同文件数量 -1
     * 
     * @param id 劳务工-合同附件主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Integer id) {
        SiteLabourStaffContractAnnex annex = mapper.getModelById(id);
        SiteLabourStaffContract contract = contractMapper.getModelByUuid(annex.getUuid());
        String filePath = MasterConfig.getLabourStaffPath()
                + "/" + contract.getIdNo() + "/contract/" + annex.getUuid() + "/" + annex.getRefilename();
        FileUtils.deleteFile(filePath);
        mapper.delete(id);
        contractMapper.subtractFileCount(annex.getUuid());
        return 1;
    }
}
